package com.documents.main.action;

import com.documents.common.core.PageBean;
import com.documents.common.core.action.BaseActionSupport;
import com.documents.htmlobj.PropertyObj;
import com.documents.pojo.DocumentGroup;
import com.documents.pojo.DocumentType;
import com.documents.service.document.DocumentGroupService;
import com.documents.service.document.DocumentTypeService;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.struts2.interceptor.ServletRequestAware;

public class DocumentCatAction extends BaseActionSupport implements ServletRequestAware {

    private static final long serialVersionUID = 1L;
    private DocumentTypeService documentTypeService;
    private DocumentGroupService documentGroupService;
    private List<DocumentType> allDocumentType;
    private List<DocumentType> documentTypeList;
    private List<DocumentGroup> documentPropertyList;
    private DocumentGroup property;
    private PageBean pageBean;
    private int pageSize = 20;
    private int page=1;
    private DocumentType cat;
    private String jsonDataString;
    private String actionURI;
    private String groupIdString;
    private Integer flag = 0;
    private Integer pid = 0;
    private int catId;
    private List<PropertyObj> propertyList;

    public List<PropertyObj> getPropertyList() {
        return propertyList;
    }

    public void setPropertyList(List<PropertyObj> propertyList) {
        this.propertyList = propertyList;
    }

    public DocumentType getCat() {
        return cat;
    }

    public void setCat(DocumentType cat) {
        this.cat = cat;
    }

    public List<DocumentType> getDocumentTypeList() {
        return documentTypeList;
    }

    public void setDocumentTypeList(List<DocumentType> documentTypeList) {
        this.documentTypeList = documentTypeList;
    }

    public DocumentGroup getProperty() {
        return property;
    }

    public void setProperty(DocumentGroup property) {
        this.property = property;
    }

    public int getCatId() {
        return catId;
    }

    public void setCatId(int catId) {
        this.catId = catId;
    }

    public DocumentGroupService getDocumentGroupService() {
        return this.documentGroupService;
    }

    public void setDocumentGroupService(DocumentGroupService documentGroupService) {
        this.documentGroupService = documentGroupService;
    }
  

    public List<DocumentGroup> getDocumentPropertyList() {
        return documentPropertyList;
    }

    public void setDocumentPropertyList(List<DocumentGroup> documentPropertyList) {
        this.documentPropertyList = documentPropertyList;
    }

    public String getGroupIdString() {
        return groupIdString;
    }

    public void setGroupIdString(String groupIdString) {
        this.groupIdString = groupIdString;
    }

    public String getActionURI() {
        return actionURI;
    }

    public void setActionURI(String actionURI) {
        this.actionURI = actionURI;
    }

    public String main() throws Exception {
        return "success";
    }

    public String top() throws Exception {
        allDocumentType = documentTypeService.findAll();
        return "success";
    }

    public String deleteById() {

        documentTypeService.deleteById(cat.getId());
        return SUCCESS;
    }

    public String list() throws Exception {

        pageBean = documentTypeService.getPageBean(page, pageSize, cat);

        return SUCCESS;
    }

    public String addInput() throws Exception {
        actionURI = "save";
        propertyList = documentGroupService.createList();
        return SUCCESS;
    }

    public String selectProperty() throws Exception {

        this.documentPropertyList = documentGroupService.getPropertyList();

        return SUCCESS;
    }

    public String save() throws Exception {

        documentTypeService.saveInstance(cat, property);
        return SUCCESS;
    }

    public String update() throws Exception {
        if (property != null) {
            Set groupList = new HashSet();
            groupList.add(property);
            cat.setDocumentGroups(groupList);
        }
        documentTypeService.update(cat);
        return SUCCESS;
    }

    public String updateInput() throws Exception {
        actionURI = "update";
        cat = this.documentTypeService.get(catId);
        Set groupList = cat.getDocumentGroups();
        if (groupList != null) {
            Iterator it = groupList.iterator();
            while (it.hasNext()) {
                property = (DocumentGroup) it.next();
            }
        }
        propertyList = documentGroupService.createList();
        if (cat == null) {
            return ERROR;
        } else {
            return SUCCESS;
        }
    }

    public List<DocumentType> getAllDocumentType() {
        return allDocumentType;
    }

    public void setAllDocumentType(List<DocumentType> allDocumentType) {
        this.allDocumentType = allDocumentType;
    }

    public DocumentTypeService getDocumentTypeService() {
        return documentTypeService;
    }

    public void setDocumentTypeService(DocumentTypeService documentTypeService) {
        this.documentTypeService = documentTypeService;
    }

    public Integer getFlag() {
        return flag;
    }

    public void setFlag(Integer flag) {
        this.flag = flag;
    }

    public String getJsonDataString() {
        return jsonDataString;
    }

    public void setJsonDataString(String jsonDataString) {
        this.jsonDataString = jsonDataString;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public PageBean getPageBean() {
        return pageBean;
    }

    public void setPageBean(PageBean pageBean) {
        this.pageBean = pageBean;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getPid() {
        return pid;
    }

    public void setPid(Integer pid) {
        this.pid = pid;
    }
}
